<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>近似相等比较器</title>
    <style>
        /* 页面样式 */
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f4f8;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            color: #333;
        }

        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
            width: 300px;
        }

        h1 {
            color: #4CAF50;
            font-size: 24px;
        }

        .input-group {
            margin: 15px 0;
        }

        input[type="number"] {
            padding: 10px;
            font-size: 16px;
            width: 100px;
            margin-right: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            padding: 15px;
            font-size: 18px;
            border-radius: 4px;
            display: inline-block;
        }

        .equal {
            background-color: #4CAF50;
            color: white;
        }

        .not-equal {
            background-color: #f44336;
            color: white;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>近似相等比较器</h1>
        <div class="input-group">
            <input type="number" id="inputA" placeholder="请输入数字A">
            <input type="number" id="inputB" placeholder="请输入数字B">
        </div>
        <button onclick="checkEquality()">比较</button>
        <div id="result" class="result"></div>
    </div>

    <script>
        // 定义近似相等的比较函数
        const approximatelyEqual = (a, b, epsilon = 0.001) => Math.abs(a - b) < epsilon;

        // 检查并显示比较结果
        function checkEquality() {
            // 获取用户输入的两个数字
            const a = parseFloat(document.getElementById('inputA').value);
            const b = parseFloat(document.getElementById('inputB').value);

            // 获取结果显示区域
            const resultElement = document.getElementById('result');

            // 判断输入是否有效
            if (isNaN(a) || isNaN(b)) {
                resultElement.textContent = "请输入有效的数字！";
                resultElement.className = "result not-equal";
            } else {
                // 使用近似相等的比较函数
                if (approximatelyEqual(a, b)) {
                    resultElement.textContent = "这两个数字近似相等！";
                    resultElement.className = "result equal";
                } else {
                    resultElement.textContent = "这两个数字不近似相等！";
                    resultElement.className = "result not-equal";
                }
            }
        }
    </script>
</body>

</html>